package com.example.fido2013;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class BroadcastChoiceScreen extends Activity {

	String lat;
	String lon;
	String bombType;
	String bombIsActive;
	String dogID;
	String dogvest;
	
	String sessionName;
	String sessionId;
	
	String alertingActivity;
	
	ArrayList<JSONObject> userTypeList;
	
	CheckBox policeCheckBox;
	CheckBox firemenCheckBox;
	CheckBox k9CheckBox;
	CheckBox allCheckBox;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_broadcast_choice_screen);
		
		Intent thisIntent = getIntent();
		lat = thisIntent.getStringExtra("lat");
		lon = thisIntent.getStringExtra("lon");
		bombType = thisIntent.getStringExtra("bombType");
		bombIsActive = thisIntent.getStringExtra("isActive");
		dogID = thisIntent.getStringExtra("dogID");
		dogvest = thisIntent.getStringExtra("dogvest");
		
		alertingActivity = thisIntent.getStringExtra("alerting activity");
		
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(BroadcastChoiceScreen.this);
		sessionName = prefs.getString("sessionName", "null");
		sessionId = prefs.getString("sessionId", "null");
		
		APICallAsync apiCall = new APICallAsync("http://dev.m.gatech.edu/d/vkrishnan35/w/fidocpy/c/api/usertype", sessionName, sessionId, BroadcastChoiceScreen.this);
		try {
			userTypeList = apiCall.execute().get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		//makeCheckBoxGroup(); 
		
		// TODO make this dynamic thing work
		
		policeCheckBox = (CheckBox) findViewById(R.id.checkBox1);
		firemenCheckBox = (CheckBox) findViewById(R.id.checkBox2);
		k9CheckBox = (CheckBox) findViewById(R.id.checkBox3);
		allCheckBox = (CheckBox) findViewById(R.id.checkBox4);
		
		allCheckBox.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {				
				if (((CheckBox) v).isChecked()) {
					policeCheckBox.setChecked(true);
					k9CheckBox.setChecked(true);
					firemenCheckBox.setChecked(true);
				}
				else {
					policeCheckBox.setChecked(false);
					k9CheckBox.setChecked(false);
					firemenCheckBox.setChecked(false);
				}
			}
			
		});
		
	}
	
	public void broadcastAlert(View view) {		
		boolean police = policeCheckBox.isChecked();
		boolean firemen = firemenCheckBox.isChecked();
		boolean k9 = k9CheckBox.isChecked();
		boolean all = allCheckBox.isChecked();
		
		String userTypes = "";
		
		if (all) {
			userTypes = userTypes + "PFH";
		}
		else { 
			if (police) {
				userTypes = userTypes + "P";
			}
			if (firemen) {
				userTypes = userTypes + "F";
			}
			if (k9) {
				userTypes = userTypes + "H";
			}
		}
		
		System.out.println(userTypes);
		
		if (userTypes.equals("")) {
			Toast.makeText(BroadcastChoiceScreen.this, "Please make a selection to broadcast this alert.", Toast.LENGTH_LONG).show();
		}
		else {		
			BroadcastAlertTask task = new BroadcastAlertTask(sessionName, sessionId, BroadcastChoiceScreen.this, userTypes, lat, lon, bombType, bombIsActive, dogID, dogvest);
			String finished = null;
			try {
				finished = task.execute().get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
			
			if (finished.equals("true")) {
				finish();
			}
			else {
				Toast.makeText(BroadcastChoiceScreen.this, "A problem occurred.", Toast.LENGTH_LONG).show();
			}
		}				
	}
	
	public void makeCheckBoxGroup() {
		int numUserTypes = userTypeList.size();
		final CheckBox[] checkBoxes = new CheckBox[numUserTypes];
		
		ViewGroup thisScreen = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_broadcast_choice_screen, null);
		RelativeLayout layout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_broadcast_choice_screen, thisScreen);

		
		for (int i = 0; i < numUserTypes; i++) {
			checkBoxes[i] = new CheckBox(this);
			String userType = "";
			try {
				userType = userTypeList.get(i).getString("userType");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			checkBoxes[i].setText(userType);
			layout.addView(checkBoxes[i]);
			System.out.println(userType);
		}
		
		setContentView(layout);
	}
}
